/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package models;

import java.math.BigInteger;
import java.security.MessageDigest;
import java.sql.SQLException;

/**
 *
 * @author 40800431
 */
public class Funcionario {

    private int id;
    private String nome;
    private String ramal;
    private String cargo;
    private double salario;
    private String email;
    private String senha;
    
    public Funcionario(){}
    
    public Funcionario(String nome_funcionario) {
        this.load(nome_funcionario);
    }
    
    public static String[] listAllAsArray() {
        
        Database db = new Database();
        String[] funcionarioList;
        db.connect();
        
        int rowCount = 0;
                
        try {
            db.query("SELECT count(*) FROM Funcionarios");
            db.res.next();
            rowCount = db.res.getInt(1);
            
            db.query("SELECT nome FROM Funcionarios ORDER BY nome");
        
            funcionarioList = new String[rowCount];
            for(int i = 0; i < rowCount; i++){
                db.res.next();
                funcionarioList[i] = db.res.getString("nome");
            }

            db.disconnect();
            
            return funcionarioList;
        } catch (Exception e) {
            e.printStackTrace();
        }
        
        return null;
    }
    
    public boolean Authenticate(){
        Database db = new Database();
        db.connect();
        
        try{
            db.query("SELECT COUNT(*) FROM Funcionarios WHERE email = '" + this.email + "' AND senha = '" + this.senha + "'");
            
            int count = 0;
            while(db.res.next()){
                count++;
            }
            
            if(count == 1){
                db.query("SELECT id_funcionario, nome, email FROM Funcionarios WHERE email = '" + this.email + "' AND senha = '" + this.senha + "' LIMIT 1");
                
                db.res.next();
                
                this.setId(db.res.getInt("id_funcionario"));
                this.setNome(db.res.getString("nome"));
                this.setEmail(db.res.getString("email"));
                
                return true;
            }else{
                return false;
            }
            
            
        }catch(SQLException e){
            System.out.println(e);
        }finally{
            db.disconnect();
        }
        return false;
        
    }
    
    public boolean Authenticate(String email, String senha){
        this.setEmail(email);
        this.setSenha(senha);
        
        return this.Authenticate();
    }
    
    public int getId(){
        return id;
    }
    
    public void setId(int id){
        this.id = id;
    }
    
    /**
     * @return the nome
     */
    public String getNome() {
        return nome;
    }

    /**
     * @param nome the nome to set
     */
    public void setNome(String nome) {
        this.nome = nome;
    }

    /**
     * @return the ramal
     */
    public String getRamal() {
        return ramal;
    }

    /**
     * @param ramal the ramal to set
     */
    public void setRamal(String ramal) {
        this.ramal = ramal;
    }

    /**
     * @return the cargo
     */
    public String getCargo() {
        return cargo;
    }

    /**
     * @param cargo the cargo to set
     */
    public void setCargo(String cargo) {
        this.cargo = cargo;
    }

    /**
     * @return the salario
     */
    public double getSalario() {
        return salario;
    }

    /**
     * @param salario the salario to set
     */
    public void setSalario(double salario) {
        this.salario = salario;
    }

    /**
     * @return the login
     */
    public String getEmail() {
        return email;
    }

    /**
     * @param login the login to set
     */
    public void setEmail(String email) {
        this.email = email;
    }

    /**
     * @param senha the senha to set
     */
    public void setSenha(String senha) {
         try{
            byte[] bytesOfPassword = senha.getBytes("UTF-8");
            MessageDigest md = MessageDigest.getInstance("MD5");

            md.update(bytesOfPassword, 0, senha.length());
            
            this.senha = (new BigInteger(1,md.digest()).toString(16));
        }catch(Exception e){
            e.getMessage();
        }
        
        
    }

    private void load(String nome_funcionario) {
       String sqlQuery = "SELECT * FROM Funcionarios WHERE nome = '" + nome_funcionario + "' LIMIT 1";
       
        
        Database db = new Database();
        db.connect();
        db.query(sqlQuery);
        
        try{
            db.res.next();
            this.setId(db.res.getInt("id_funcionario"));
            this.setNome(db.res.getString("nome"));
            this.setEmail(db.res.getString("email"));
            this.setRamal(db.res.getString("ramal"));
            this.setSalario(db.res.getDouble("salario"));
        }catch(Exception e){
            e.printStackTrace();
        }finally{
            db.disconnect();
        }
    }
    
}
